import Vue from 'vue'
import Router from 'vue-router'

// 引入一级组件
import Dashboard from '../views/dashboard/Dashboard.vue'

// 懒加载二级组件 Tarbar
const Home = () => import('../views/home/Home.vue');
const Category = () => import('../views/category/Category.vue');
const Eat = () => import('../views/eat/Eat.vue');
const Cart = () => import('../views/cart/Cart.vue');
const Mine = () => import('../views/mine/Mine.vue');

// 加载订单相关的组件
// const Order = () => import('../views/order/Order.vue');


// 我的订单
const MyOrder = () => import('../views/mine/Children/MyOrder');
// 优惠券
const CouponList = () => import('../views/mine/Children/CouponList.vue')
// 语言切换
const SwitchLanguage = () => import('../views/mine/Children/SwitchLanguage.vue');


const Order = () => import('../views/order/Order.vue');
const MyAddress = () => import('../views/order/children/MyAddress.vue');
const AddAddress = () => import('../views/order/children/children/AddAddress.vue');
const EditAddress = () => import('../views/order/children/children/EditAddress.vue');

// 注册登录
const Login = () => import('../views/login/Login.vue');

// 地图
const Map = () => import('../views/home/components/map/Map.vue');

// 解决多次点击重复路由报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const router = new Router({
  routes: [{
    path: '/',
    redirect: '/dashboard',
    // 是否数据缓存
    meta: {
      keepAlive: true
    },
  }, {
    // 根页面 
    path: '/dashboard',
    name: 'dashboard',
    component: Dashboard,
    children: [
      {
        path: '/dashboard',
        redirect: '/dashboard/home',
        // 是否数据缓存
        meta: {
          keepAlive: true
        },
      }, {
        // 主页
        path: 'home',
        name: 'home',
        component: Home,
        // 是否数据缓存
        meta: {
          keepAlive: true
        }
      }, {
        // 分类
        path: 'category',
        name: 'category',
        component: Category,
        // 是否数据缓存
        meta: {
          keepAlive: true
        },
      }, {
        // 吃什么
        path: 'eat',
        name: 'eat',
        component: Eat,
        // 是否数据缓存
        meta: {
          keepAlive: true
        }
      }, {
        // 购物车
        path: 'cart',
        name: 'cart',
        component: Cart,
        meta: {
          keepAlive: true
        }
      }, {
        path: 'mine',
        name: 'mine',
        component: Mine,
        children: [{
          // 优惠券
          path: 'couponList',
          name: 'couponList',
          component: CouponList,
          meta: {
            requireAuth: true
          }
        }, {
          // 我的订单
          path: 'myOrder',
          name: 'myOrder',
          component: MyOrder,
          meta: {
            requireAuth: true
          }
        },{
          path: 'switchLanguage',
          name: 'switchLanguage',
          component: SwitchLanguage
      }]
      }, {
        path: 'map',
        name: 'map',
        component: Map,
      }
    ]
  }, {
    // 订单
    path: '/order',
    name: 'order',
    component: Order,
    children: [
      {
        // 我的地址
        path: 'myAddress',
        name: 'myAddress',
        component: MyAddress,
        meta: {
          requireAuth: true
        },
        children: [
          {
            // 添加地址
            path: 'addAddress',
            name: 'addAddress',
            component: AddAddress,
          }, {
            // 编辑地址
            path: 'editAddress',
            name: 'editAddress',
            component: EditAddress
          }
        ]
      },
    ]
  }, {
    // 登录
    path: '/Login',
    name: 'login',
    component: Login
  }]
})


export default router